package com.zhike.b2b.expand.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.zhike.b2b.Query;
import com.zhike.b2b.base.BaseService;
import com.zhike.b2b.expand.IExpandService;
import com.zhike.b2b.model.CompanyVo;
import com.zhike.b2b.model.ProductVo;
import com.zhike.b2b.model.UserInfo;
import com.zhike.b2b.util.ClassUtil;

@Service
public class ExpandService  extends BaseService implements IExpandService
{
    /**
     * 查询推广的产品(供应)列表
     * @param recommendType
     * @param posId
     * @return
     */
	public int queryProducts(List<ProductVo> lpv,int recommendType,int posId,int start,int size) {
		String sql = "SELECT RL.ROOT_ID FROM RECOMMEND_LIST RL,RECOMMEND_SITE R WHERE RL.POS_ID=R.POS_ID AND RL.START_DATE<=NOW()"+
		       " AND RL.END_DATE >=NOW() AND RECOMMEND_STATE =1 AND RL.RECOMMEND_TYPE=? AND RL.POS_ID=? ";
		int total = 0 ;
		try {
			total = jt.queryForInt(sql,recommendType,posId);
			String limitStr = " limit "+start+","+size+" ";
			lpv = jt.query(Query.recommend_list_GET_PRODUCT_RECOMMEND_LIST+limitStr, 
					new RowMapper<ProductVo>() {
				public ProductVo mapRow(ResultSet rs, int rowNum) throws SQLException {
					ProductVo pv = new ProductVo();
					pv.setClassId(rs.getString("class_id"));
					pv.setProductId(rs.getString("product_id"));
					pv.setProductName(rs.getString("product_name"));
					pv.setStorePath(rs.getString("store_path"));
					pv.setArticleRule(rs.getString("article_rule"));
					pv.setDesc(rs.getString("product_abstract"));
					String smallImg = rs.getString("file_path");
					if(smallImg == null || smallImg.trim().length()<1)
						smallImg = defaultLogoImg;
					pv.setSmallImg(smallImg);
	                pv.setPublishTime(rs.getString("PUBLISH_DATE"));
					
					CompanyVo com = new CompanyVo();
					com.setCustId(rs.getString("cust_id"));
					com.setCustName(rs.getString("CUST_NAME"));
					com.setCustType(rs.getString("CUST_TYPE"));
					com.setAddress(rs.getString("AREA_NAME"));
					String level = rs.getString("LEVEL");
					level = ps.getCustLevelLogo(level);
					com.setLevelLogo(level);

					UserInfo ui = new UserInfo();
					ui.setContactName(rs.getString("contact_name"));
					ui.setQq(rs.getString("QQ"));
					
					pv.setUser(ui);
					pv.setCust(com);
					
					pv.setUrl(ClassUtil.getObjUrl(pv, pv.getProductId()));
					return pv;
				}
			},recommendType,posId);
		} catch (DataAccessException e) {
			logger.error(e.getMessage());
		}
		return total;
	}
}
